<template>
  <div class="container">
    <div class="editBlog">
      <div class="title">
        <label for="title">题目</label>
        <input type="text" id="title" class="form-control" placeholder="Enter title ..." v-model="title"/>
      </div>
      <div class="content">
        <label for="content">正文</label>
        <textarea id="content" class="form-control" placeholder="Enter content ..." rows="30" v-model="content"/>
      </div>
      <div class="btnspace">
        <button class="btn btn-success btn-addBlog" :style="ismodify === '0'?'':'display:none;'" @click="addBlog">添加</button>
        <button class="btn btn-success btn-addBlog" :style="ismodify === '1'?'':'display:none;'" @click="modifyBlog">修改</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'editBlog',
  data () {
    return {
      id: 0,
      title: '',
      content: '',
      writedate: '',
      ismodify: ''
    }
  },
  mounted () {
    if (this.$route.params.blogid) {
      this.ismodify = '1'
      this.id = this.$route.params.blogid
      this.$http.post('/java_kunpeng/getModifyBlog', {
        id: this.id
      })
        .then((response) => {
          const json = response.data
          if (json.state === '0') {
            this.title = json.info.title
            this.content = json.info.content
            this.writedate = json.info.writedate
          }
        })
        .catch((error) => {
          alert(error.toString())
        })
    } else {
      this.ismodify = '0'
    }
  },
  methods: {
    addBlog: function () {
      this.$http.post('/java_kunpeng/addBlog', {
        title: this.title,
        content: this.content
      })
        .then((response) => {
          const json = response.data
          if (json.state === '0') {
            this.$router.push('/selfBlog')
          } else {
            alert('添加失败!' + json.msg)
          }
        })
        .catch((error) => {
          alert(error.toString())
        })
    },
    modifyBlog: function () {
      this.$http.post('/java_kunpeng/modifyBlog', {
        id: this.id,
        title: this.title,
        content: this.content
      })
        .then((response) => {
          const json = response.data
          if (json.state === '0') {
            this.$router.push('/selfBlog')
          } else {
            alert('error' + json.msg)
          }
        })
        .catch((error) => {
          alert(error.toString())
        })
    }
  }
}
</script>

<style scoped>
  .editBlog {
    width: 80%;
    margin: auto;
  }
</style>
